--Skill Status Bar
local SkillEvent = MClass("SkillEvent")

local specialBrokenSignPanelWidth = 148 -- 特殊打断条 panel宽
local offsetY = 40

--ctor 初始化UI init设置静态数据 setData设置动态数据
function SkillEvent:ctor()
    --self.root 默认为根节点
    self.pro = self:findChild("Panel/Slider_1") or self:findChild("Panel/Slider")
    self.proAccDamage = self:findChild("Panel/LoadingBar_2")
    self.specialBrokenSign = self:findChild("Panel/LoadingBar_2/Panel")
    if self.specialBrokenSign then
        specialBrokenSignPanelWidth = self.specialBrokenSign:getContentSize2()
    end
    self.proAccDamageBg = self:findChild("Panel/bg_2")
    self.text = self:findChild("Panel/Text")
    self.proPanel = self:findChild("Panel")
    self.rollCDPanel = self:findChild("Panel_roll")
    self.rollCDProgress = self:findChild("Panel_roll/Slider")
    self.rollCDText = self:findChild("Panel_roll/Text")
	self:init()
end


function SkillEvent:init()
    self:stopAllActions()
    self.bShowProcess = false
    self.bShowRollCD = false
    if self.proPanel then
        self.proPanel:setVisible(false)
    end
    if self.rollCDPanel then
        self.rollCDPanel:setVisible(false)
    end
    BattleUIEvent:addEventListener(BattleUIEvent.TYPE.SKILL_PROCESS, handler(self,SkillEvent.setData))
    --BattleUIEvent:addEventListener(BattleUIEvent.TYPE.SKILL_ROLLCD, handler(self,SkillEvent.setRollCD))
    --BattleUIEvent:addEventListener(BattleUIEvent.TYPE.SKILL_ROLLCD_HINT, handler(self,SkillEvent.playRollCDHintEffect))
end

function SkillEvent:setData(bindObj,bShow,name,percent, accDamagePercent, isSpecialBrokenType, beBrokenAccDamageMax)
    if bindObj == self.bindObj and self.proPanel then
        if percent and percent >= 100 then
            self.bShowProcess = false
        else
            self.bShowProcess = bShow
        end
        self.proPanel:setVisible(self.bShowProcess)
        if self.bindObj ~= "mainui" then
            self.bindObj:updateSkillEventNodePosition()
        end
        if not self.bShowProcess then
            return
        end

        self.text:setString(name)

        self.pro:setPercent(percent)

        if self.proAccDamage then
            if accDamagePercent == nil then
                self.proAccDamage:setVisible(false)
                self.proAccDamageBg:setVisible(false)
            else
                self.proAccDamage:setVisible(true)
                self.proAccDamageBg:setVisible(true)
            end
            accDamagePercent = accDamagePercent or 0
            self.proAccDamage:setPercent(accDamagePercent)

            if self.specialBrokenSign and isSpecialBrokenType then -- 特殊打断条
                self.specialBrokenSign:setVisible( isSpecialBrokenType )
                if not self._specialHPInitEnd then
                    local shownNum = 0
                    local nodeListTemp = {}
                    for i = 1, 9 do
                        local ndoeTemp = self.specialBrokenSign:getChildByName( "Image_" .. i )
                        table.insert( nodeListTemp, ndoeTemp )

                        if i < beBrokenAccDamageMax then
                            shownNum = shownNum + 1
                            ndoeTemp:setVisible( true )
                        else
                            ndoeTemp:setVisible( false )
                        end
                    end
                    local distanceX = specialBrokenSignPanelWidth/( shownNum + 1 )
                    for i,v in ipairs( nodeListTemp ) do
                        local posX = 0 + distanceX*i
                        v:setPositionX( posX )
                    end
                    self._specialHPInitEnd = true
                end
            end
        end
    end
end

function SkillEvent:setRollCD(bindObj, bShow, percent, cd)
    if bindObj == self.bindObj and self.rollCDPanel then
        if percent and (percent >= 100 or percent <= 0) then
            self.bShowRollCD = false
        else
            self.bShowRollCD = bShow
        end
        self.rollCDPanel:setVisible(self.bShowRollCD)
        if self.bindObj ~= "mainui" then
            self.bindObj:updateSkillEventNodePosition()
        end
        if not self.bShowRollCD then
            return
        end

        self.rollCDProgress:setPercent(percent)
        self.rollCDText:setString(string.format("%0.1f", cd/1000))
        if self.bShowProcess then
            self.rollCDPanel:setPositionY(offsetY)
        else
            self.rollCDPanel:setPositionY(0)
        end
    end
end

function SkillEvent:playRollCDHintEffect()
    local action = cc.CSLoader:createTimeline(Res.Node_skillevent)
    if action then
        self.root:stopAllActions()
        self.root:runAction(action)
        action:gotoFrameAndPlay(0, false)
    end
end

function SkillEvent:bindEvent(bindObj)
    self.bindObj = bindObj
end

function SkillEvent:isShow()
    return self.bShowProcess or self.bShowRollCD
end

return SkillEvent
